package com.hgyc.mom.system.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.common.enums.CommonStatus;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.system.entity.SysPost;
import com.hgyc.mom.system.vo.SysPostParam;
import com.hgyc.mom.system.service.SysPostService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 岗位信息Controller
 *
 * @author fwj
 * @since 2025-05-15
 */
@Tag(name = "岗位信息控制器", description = "岗位信息控制器")
@RestController
@RequestMapping("/system/post")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    @Operation(summary = "查询岗位信息列表", description = "查询岗位信息列表")
    @PostMapping("/pageList")
    public Result<IPage<SysPost>> pageList(@Parameter(description = "查询参数") @RequestBody SysPostParam sysPostParam) {
        Page<SysPost> page = new Page<>(sysPostParam.getPage(), sysPostParam.getPageSize());
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostParam, sysPost);
        QueryWrapper<SysPost> wrapper = QueryWrapperUtil.build(sysPost);
        IPage<SysPost> list = sysPostService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询岗位信息列表", description = "查询岗位信息列表")
    @GetMapping("/list")
    public Result<List<SysPost>> list() {
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPost::getStatus, CommonStatus.ENABLE.getValue());
        return Result.success(sysPostService.list(wrapper));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysPost> util = new ExcelUtil<>(SysPost.class);
        util.importTemplateExcel(response, "岗位信息");
    }

    @Operation(summary = "导出岗位信息列表", description = "导出岗位信息列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostParam sysPostParam) {
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostParam, sysPost);
        QueryWrapper<SysPost> wrapper = QueryWrapperUtil.build(sysPost);
        List<SysPost> list = sysPostService.list(wrapper);
        ExcelUtil<SysPost> util = new ExcelUtil<>(SysPost.class);
        util.exportExcel(response, list, "岗位信息数据");
    }

    @Operation(summary = "导入岗位信息列表", description = "导入岗位信息列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysPost> util = new ExcelUtil<>(SysPost.class);
        List<SysPost> sysPostList = util.importExcel(file.getInputStream());
        String message = sysPostService.importData(sysPostList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取岗位信息详细信息", description = "获取岗位信息详细信息")
    @GetMapping(value = "/{postId}")
    public Result getInfo(@Parameter(description = "岗位信息ID") @PathVariable("postId") Long postId) {
        return Result.success(sysPostService.getById(postId));
    }

    @Operation(summary = "新增岗位信息", description = "新增岗位信息")
    @PostMapping
    public Result save(@Parameter(description = "岗位信息") @RequestBody SysPost sysPost) {
        return Result.success(sysPostService.save(sysPost));
    }

    @Operation(summary = "修改岗位信息", description = "修改岗位信息")
    @PutMapping
    public Result update(@Parameter(description = "岗位信息") @RequestBody SysPost sysPost) {
        return Result.success(sysPostService.updateById(sysPost));
    }

    @Operation(summary = "删除岗位信息", description = "删除岗位信息")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] postIds) {
        return Result.success(sysPostService.removeByIds(Arrays.asList(postIds)));
    }
}